package com.gongj.appuser.controller;

import Clients.IUserClient;
import com.gongj.commons.pojo.User;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Value("${server.port}")
    int port;

    public String getUser(){
        System.out.println(new Date() + ">>>" );
        //int i  =1 /0;
        return "调用成功: =====" + port;
    }

    @GetMapping("/getName")
    public String getName(String name) {
        System.out.println(new Date() + ">>>" + name);
        return "调用成功: =====" + name + "=====" + new Date();
    }

    @PostMapping("/addUser")
    public User addUser(User user){
        System.out.println(user);
        return user;
    }

    @PostMapping("/addUserJson")
    public User addUserJson(@RequestBody User user){
        System.out.println(user);
        return user;
    }

    @PutMapping("/put/{id}")
    public User put(@PathVariable Integer id,@RequestBody User user){
        System.out.println(id + "=="+user);
        return user;
    }

    @DeleteMapping("/del/{id}")
    public void del(@PathVariable("id") String id){
        System.out.println(id);
    }



    public String getUserInfo(@PathVariable("id") String userId){
        return "id:" + userId;
    }

    public String findByLikeName(@RequestHeader String name) throws UnsupportedEncodingException {
        return "name:" + URLDecoder.decode(name,"UTF-8");
    }

    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/img") + format;
        System.out.println(realPath);
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        try {
            file.transferTo(new File(folder, newName));
           // String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/img" + //format + newName;
            return realPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }


//    //用来测试hystrix的请求合并
    @GetMapping("/getUserByIds/{ids}")
    public List<User> getUserByIds(@PathVariable String ids){
        System.out.println("===appUser====ids==="+ ids);
        String[] split = ids.split(",");
        List<User> users = new ArrayList<>();
         for (String s : split) {
             User u = new User();
             u.setId(s);
             users.add(u);
         }
         return users;
   }
   
}
